package com.lnsf.bookstore.common.util;

import com.lnsf.bookstore.common.util.StringHelper;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.Properties;

/**
 * 配置文件处理类 
 * 
 * @author lijunjie
 * 
 * 
 */
public class PropertyUtil {
	/**
	 * 获取属性文件api.properties  的配置
	 * @param key 
	 * @return
	 */
	public static String getApiProperty(String key) {
		InputStream in = PropertyUtil.class.getResourceAsStream("/api.properties");
		Properties p = new Properties();
		try {
			p.load(in);
		} catch (IOException e) {
			e.printStackTrace();
		}
		return p.getProperty(key);
	}

	/**
	 * 获取对应文件夹下的配置文件
	 * 
	 * @param dirName
	 * @return
	 */
	public static Properties getPropertes(String dirName) {

		InputStream in = PropertyUtil.class.getResourceAsStream("/" + dirName + ".properties");

		Properties p = new Properties();
		try {
			p.load(in);
		} catch (IOException e) {
			e.printStackTrace();
		}
		return p;
	}

	/**
	 * 获取dirName下fileName的配置文件
	 *
	 * @param dirName 文件夹名
	 * @param fileName 文件名
	 * @return
	 */
	public static Properties getPropertes(String dirName, String fileName) {
		InputStream in = PropertyUtil.class.getResourceAsStream("/" + dirName + "/"+fileName+".properties");
		Properties p = new Properties();
		try {
			p.load(in);
		} catch (IOException e) {
			e.printStackTrace();
		}
		return p;
	}


	/**
	 * 获取分目录下配置文件里对应的值
	 * @param dirName config下文件夹的名字 与properties文件名对应
	 * @param key 属性名
	 * @return
	 */
	public static String getPropertyValue(String dirName, String key) {
		InputStream in = PropertyUtil.class.getResourceAsStream("/" + dirName +".properties");
		
		Properties p = new Properties();
		try {
			p.load(in);
		} catch (IOException e) {
			e.printStackTrace();
		}

		return p.getProperty(key);
	}

	/**
	 * 查找键值
	 * @param key属性文件键
	 * @param
	 * @return
	 */
	public static String getPropertyByPath(String key) {
		Properties p = new Properties();
		return p.getProperty(key);
	}
}
